package servlet;

import mapper.StudentMapper;
import org.apache.ibatis.session.SqlSession;
import pojo.Student;
import utils.MybatisUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author leiHua
 * @Date 2022/11/24 17:28
 */
@WebServlet("/AddStudentServlet")
public class AddStudentServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8;");
        request.setCharacterEncoding("utf-8");
        String stuno = request.getParameter("stuno");
        String stuname = request.getParameter("stuname");
        String sex = request.getParameter("sex");
        System.out.println(sex);
        Student student = new Student();
        student.setMajor("07");
        student.setName(stuname);
        student.setNum(stuno);
        student.setGender(sex);
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        int i = mapper.insertStudent(student);
        if (i>0){
            response.sendRedirect("ShowAllStudentsServlet");
        }else {
            response.getWriter().write("添加失败");
        }
    }
}
